#ifndef _HELPER_FUNCTIONS_HPP
#define _HELPER_FUNCTIONS_HPP

#include <lua.hpp>
#include <iostream>
#include <stdarg.h>

namespace lua
{

static inline void pushCopy(lua_State *src, int srcIdx, lua_State *dest)
{
	switch(lua_type(src, srcIdx))
	{
		case(LUA_TBOOLEAN):
			lua_pushboolean(dest, lua_toboolean(src, srcIdx));
			break;
		case(LUA_TSTRING):
			lua_pushstring(dest, lua_tostring(src, srcIdx));
			break;
		case(LUA_TNUMBER):
			lua_pushnumber(dest, lua_tonumber(src, srcIdx));
			break;
		case(LUA_TNIL):
			lua_pushnil(src);
			break;
	}
}

static inline void move(lua_State* L, int idx)
{
	lua_insert(L, idx);
}

static inline void report_errors(lua_State* L, int status)
{
	if(status != 0)
	{
		std::cerr<<"Error: "<<lua_tostring(L, -1)<<std::endl;
		lua_pop(L, 1); // remove error message from stack
	}
}

static inline bool isString(lua_State *L, int idx)
{
	return lua_isstring(L, idx) == 1 ? true : false;
}

static inline int error(lua_State *L, const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	int result = luaL_error(L, fmt, args);
	va_end(args);
	return result;
}

static inline const char* typeName(lua_State *L, int idx)
{
	return luaL_typename(L, idx);
}

}

#endif